<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<style type="text/css">
<!--
/****/
	td,th{height:20px;line-height:20px;text-align:center;}
	
-->
</style>
<script type="text/javascript">
<!--
var orderdetailTableId = "orderdetailTable";
var currTr;
var table;
var index = 0;

function compute(){
	
	var url=ctx+"/common/compute.action?action=orderForm";
	var table = $("#"+orderdetailTableId);

	//仅有三行，没有定单明细时，不用计算
	if(table.get(0).rows.length<4) return;

	var para = makePara(table.get(0));
	para="&val="+para;
	$.get(url,para,function(txt){
		fillMoney(table.get(0),txt);
		},
		"text"
	);
}

function makePara(table){
	var tr = table.rows;
	var para = "";
	for(var i=0;i<tr.length;i++){
		var price = $(".price",tr[i]);
		var count = $(".count",tr[i]);
		if(price.length>0){
			para+=","+price.val();
			para+=","+count.val();
			}
		}
	return para.substr(1);
}
function fillMoney(table,moneyStr){
	var tr = table.rows;
	var moneyArr = moneyStr.split(",");
	var m=0;
	
	//填充小计
	for(var i=0;i<tr.length;i++){
		var money = $(".money",tr[i]);
		var cash = $(".goodscash",tr[i]);
		if(money.length>0){
			money.html(moneyArr[m]);
			cash.val(moneyArr[m]);
			m++;
			}
		}

	//填充合计
	$("#monyNO",table).html(moneyArr[m]);
	$("input[name='totalcash']").val(moneyArr[m]);
	
	var cashCN = convertCurrency(moneyArr[m])+"整";
	$("#monyWord",table).html(cashCN);
	$("input[name='cashcn']").val(cashCN);

}


function insert(){
	table = $("#"+orderdetailTableId);
	currTr = table.find("tr:last").prev();
	goodsDlg();
}
function goodsDlg(){
	var url= "${ctx }/crm/goods!selectGoods.action?selectType=radio&onSelect=onSelectGoods";
	$.pdialog.open(url, "selectGoods", "选择产品", {width: 800, height: 480});
}
function onSelectGoods(goods){
	insertLine();
	var newLine = currTr.prev();
	var prop;
	//根据对应属性名的className，填充值
	for(prop in goods){
		$(newLine).find("."+prop).each(function(){
			if(typeof(this.value)!="undefined"){
				this.value = goods[prop];
				}
			else{
				$(this).html(goods[prop]);
				}

			});
		}
	//定位焦点到商品数量上
	$(newLine).find(".count").focus();
	$(newLine).find(".count").select();
	
	//调整索引
	reIndexDetailArray();
	//重新计算金额
	compute();
	
}
function insertLine(){
	currTr.before($("#orderlineTemplate").html());
}
function deleteLine(){
	var curr = event.srcElement;
	var index = $(curr).parents("tr").attr("rowIndex");
	table = $("#"+orderdetailTableId);
	table.get(0).deleteRow(index);
	//调整索引
	reIndexDetailArray();

	//重新计算金额
	compute();
}
//重置表单里detail的数组索引
//在删除或添加一行后，需重置
function reIndexDetailArray(){
	table = $("#"+orderdetailTableId);
	table.find("tr").each(function(){
		var index = this.rowIndex-1;
		var input = $(this).find("input").each(function(){
			var name = this.name;
			var reg = new RegExp("^detail\[[\\d]*\]","ig");
			if(reg.test(name)){
				this.name = name.replace(reg,"detail["+index+"]");
				}
			});
		});
}

function userDlg(){
	var url = "${ctx }/crm/customer!selectCustomer.action?idInput=orderInfocustomerid&nameInput=orderInfocustomerName&field=linkman&selectType=radio"
	$.pdialog.open(url, "selectCustomerDlg", "选择客户", {width: 800, height: 480});
}

//响应快捷键
function hotKey(e){

	//ctrl+shift+n 添加一行
	//alert(e.which);
	if (e.ctrlKey && e.shiftKey && e.which==14){
		insert();
		}
	//ctrl+shift+u 选择用户
	if(e.ctrlKey && e.shiftKey && e.which==21){
		userDlg();
		}
}

function confirm(){
	var type = "${statusid}";
	if(type=="1"){
		 alertMsg.error('对不起，该订单已经过收款确认，不能再修改！',{okCall:function(){
			  navTab.closeCurrentTab();
		 }});
		
	}
}

$(document).ready(function(){
	confirm();
	$(document).keypress(function(e){
		hotKey(e);
		});
});


//-->
</script>
<div class="page">
	<div class="pageContent">
		<form id="orderInfo" method="post" action="${ctx }/crm/order!save.action" class="pageForm required-validate" onsubmit="return validateCallback(this,navTabAjaxDone);">
			<input type="hidden" name="id" value="${id }"/>
			<input type="hidden" name="navTabId" value="orderList"/>
			<div class="pageFormContent" layoutH="56" >
				<div style="height:30px;line-height:30px;margin-bottom:5px;text-align:center;font-size:12pt;font-weight:bold;">
					北京至好商贸有限公司专用订单
				</div>
				<p>
					<label>客户姓名：</label>
					<input name="customerid" id="orderInfocustomerid"  type="hidden" size="30" alt="" value="${customerid}"/>
					<input name="customer"  id="orderInfocustomerName" type="text" size="30" class="required" readonly="readonly" value="${customer}"/>
					<a class="inputButton" href="${ctx }/crm/customer!selectCustomer.action?idInput=orderInfocustomerid&nameInput=orderInfocustomerName&field=linkman&selectType=radio" target="dialog" rel="selectCustomerDlg" width="800" height="480">选择客户</a>
				</p>
				<p>
					<label>订单编号 ：</label>
					<input name="code"  type="text" size="30" readonly="readonly" value="${code }" />
				</p>
				<p>
					<label> 下 单 人：</label>
					<s:if test="maker==null">
					<input name="makerid"  type="hidden" size="30" alt="" value="${currUser.id}"/>
					<input name="maker"  type="text" size="30" alt="" readonly="readonly" value="${currUser.name}"/>
					</s:if>
					<s:else>
					<input name="makerid"  type="hidden" size="30" alt="" value="${makerid}"/>
					<input name="maker"  type="text" size="30" alt="" readonly="readonly" value="${maker}"/>
					</s:else>
					
				</p>
				<p>
					<label>下单时间：</label>
					<input name="orderdate"  type="text" size="30" readonly="readonly" value="<s:date name="orderdate" format="yyyy-MM-dd HH:mm:ss"/>" />
				</p>					
				
				<table id="orderdetailTable" style="margin:10px auto 15 auto;padding:0 auto;" align="center" width="95%" cellpadding="0" cellspacing="0" border="1">
					<thead>
						<th height="25">商品名称</th>
						<th>规格型号</th>
						<th>单 位</th>						
						<th>单 价</th>
						<th>数 量</th>
						<th>金 额</th>
						<th>操作</th>
					</thead>
					<!-- 
					<tr>
						<td>
						<input type="hidden" name="detail[0].goodsid" value="9150" />
						<input type="hidden" name="detail[0].goodsname" value="神仙牌花露水" />
						神仙牌花露水</td>
						<td><input type="hidden" name="detail[0].goodsmodel" value="SXP"/>SXP</td>
						<td><input type="hidden" name="detail[0].goodsunit" value="斤"/>斤</td>
						<td><input type="text" class="price" name="detail[0].goodsprice" size="3" value="20" onblur="compute()"/></td>
						<td><input type="text" class="count" name="detail[0].goodscount" size="3" value="20" onblur="compute()"/></td>
						<td>
						<input type="hidden" class="goodscash" name="detail[0].goodscash" value="1"/>
						<span class="money">300.00</span>
						
						</td>
						<td><div class="button"><div class="buttonContent"><button type="Button" onclick="navTab.closeCurrentTab()"> 删 除 </button></div></div></td>
					</tr>
					<tr>
						<td >
						<input type="hidden" name="detail[1].goodsid" value="9150" />
						<input type="hidden" name="detail[1].goodsname" value="神仙牌花露水" />
						神仙牌花露水
						</td>
						<td><input type="hidden" name="detail[1].goodsmodel" value="SXP"/>SXP</td>
						<td><input type="hidden" name="detail[1].goodsunit" value="斤"/>斤</td>
						<td><input type="text" class="price" name="detail[1].goodsprice" size="3" value="20" onblur="compute()"></td>
						<td><input type="text" class="count" name="detail[1].goodscount" size="3" value="20" onblur="compute()"></td>
						<td>
						<input type="hidden" class="goodscash" name="detail[1].goodscash" value=""/>
						<span class="money">300.00</span>
						</td>
						<td><div class="button"><div class="buttonContent"><button type="Button" onclick="navTab.closeCurrentTab()"> 删 除 </button></div></div></td>
					</tr>
					 -->
					
					 <!-- -->
					 <s:iterator value="detail" status="stat">
					 	<tr>
					 	<td>
					 	<s:hidden name="detail[%{#stat.index}].goodsid"  value="%{detail[#stat.index].goodsid}" ></s:hidden>
					 	<s:hidden name="detail[%{#stat.index}].goodsname"  value="%{detail[#stat.index].goodsname}" ></s:hidden>
					 	<span>${goodsname}</span>					 		
					 		</td>
						<td>
							<s:hidden name="detail[%{#stat.index}].goodsmodel"  value="%{detail[#stat.index].goodsmodel}" ></s:hidden>
							<span>${goodsmodel}</span>
						</td>
						<td>
							<s:hidden name="detail[%{#stat.index}].goodsunit"  value="%{detail[#stat.index].goodsunit}" ></s:hidden>
							<span>${goodsunit}</span>
						</td>
						<td><s:textfield cssClass="price" name="detail[%{#stat.index}].goodsprice"  value="%{detail[#stat.index].goodsprice}" onblur="compute()" theme="simple" size="3" ></s:textfield></td>
						<td><s:textfield cssClass="count" name="detail[%{#stat.index}].goodscount"  value="%{detail[#stat.index].goodscount}" onblur="compute()" theme="simple" size="3"></s:textfield></td>
						<td>
						<s:hidden cssClass="goodscash" name="detail[%{#stat.index}].goodscash"  value="%{detail[#stat.index].goodscash}" ></s:hidden>
						<span class="money">${goodscash}</span>
						</td>
						<td><div class="button"><div class="buttonContent"><button type="Button" onclick="deleteLine();"> 删 除 </button></div></div></td>
					 	</tr>
					 </s:iterator>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td><div class="button"><div class="buttonContent"><button type="Button" onclick="insert();"> 添 加 </button></div></div></td>
					</tr>
					<tr>
						<td>合 计</td>	
						<td colspan="4">
						（大写） <span id="monyWord">${cashcn}</span>
						<s:hidden name="cashcn"  value="%{cashcn}" ></s:hidden>
						</td>
						<td  colspan="2">
						（小写）<span id="monyNO"> ${totalcash }</span>
						<s:hidden name="totalcash"  value="%{totalcash}" ></s:hidden>
						</td>
					</tr>
				</table>
				<div>
					<label>备 注：</label>
					<textarea rows="5" cols="80" name="note">${note }</textarea>
				</div>
			</div>
			<div class="formBar">
				<ul>
					<li><div class="buttonActive"><div class="buttonContent"><button type="button" onclick="compute();" >金额计算</button></div></div></li>
					<li><div class="button"><div class="buttonContent"><button type="submit">保存</button></div></div></li>
					<li>
						<div class="button"><div class="buttonContent"><button type="Button" onclick="navTab.closeCurrentTab()">取消</button></div></div>
					</li>
				</ul>
			</div>
		</form>
	</div>
</div>
<div id="orderlineTemplate" style="display:hidden">
					<tr>
						<td >
							<input type="hidden" class="id" name="detail[1].goodsid" value="" /><input type="hidden" class="pname" name="detail[1].goodsname" value="" /><span class="pname"></span>
						</td>
						<td><input type="hidden" class="model" name="detail[1].goodsmodel" value=""/><span class="model"></span></td>
						<td><input type="hidden" class="unit" name="detail[1].goodsunit" value=""/><span class="unit"></span></td>
						<td><input type="text" class="price" name="detail[1].goodsprice" size="3" value="0" onblur="compute()"></td>
						<td><input type="text" class="count" name="detail[1].goodscount" size="3" value="1" onblur="compute()"></td>
						<td>
						<input type="hidden" class="goodscash" name="detail[1].goodscash" value=""/>
						<span class="money"></span>
						</td>
						<td><div class="button"><div class="buttonContent"><button type="Button" onclick="deleteLine();"> 删 除 </button></div></div></td>
					</tr>
</div>